import { CommandCenter } from '../../core/commands/CommandCenter'
import { RegistryItemFactory } from '../../core/commands/RegistryItemFactory'
import type { CommandHandler, ICommandService, CommandRegistryItem } from '../../core/commands/command.type'


export class CommandService implements ICommandService{

  private uiCenter: CommandCenter;
  private serviceCenter: CommandCenter;
  constructor() { 
    this.uiCenter = new CommandCenter();
    this.serviceCenter = new CommandCenter();
  }

  registerForUi(module: string, name: string, handler: CommandHandler, context?: Record<string, any>): void {
    this.uiCenter.register(RegistryItemFactory.forUi(module, name, handler, context));
  }

  registerForService(module: string, name: string, handler: CommandHandler, context?: Record<string, any>): void {
    this.serviceCenter.register(RegistryItemFactory.forService(module, name, handler, context));
  }

  async executeForUi(module: string, commandName: string, ...params: any[]): Promise<any> {
    const item = this.uiCenter.get(module, commandName);
    await this.beforeExecute(item)
    return this.uiCenter.execute(module, commandName, ...params);
  }

  async executeForService(module: string, commandName: string, ...params: any[]): Promise<any> {
    return this.serviceCenter.execute(module, commandName, ...params);
  }

  async beforeExecute(registryItem: CommandRegistryItem|undefined): Promise<void> {
    // 切换路由
    if (registryItem != undefined) {
      return;
    }
    console.log(registryItem);
  }
}